package org.genntii.warehousesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.Exception.ParamErrorException;
import org.genntii.warehousesystem.domain.dto.WarehouseDTO;
import org.genntii.warehousesystem.domain.entity.WareHouse;
import org.genntii.warehousesystem.domain.vo.WarehouseVO;
import org.genntii.warehousesystem.mapper.CityMapper;
import org.genntii.warehousesystem.mapper.ProvinceMapper;
import org.genntii.warehousesystem.mapper.WarehouseMapper;
import org.genntii.warehousesystem.service.WarehouseService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class
WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, WareHouse> implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private CityMapper cityMapper;

    @Resource
    private ProvinceMapper provinceMapper;

    @Override
    public List<WarehouseVO> findAllUnable(int page) {
        List<WareHouse>wareHouseList = warehouseMapper.findAllUnable(10*(page-1));
        List<WarehouseVO> voList = new ArrayList<>();
        for (WareHouse wareHouse:wareHouseList){
            WarehouseVO vo = new WarehouseVO();
            int cityId = wareHouse.getCityId();
            int provinceId = cityMapper.findProvinceId(cityId);
            vo.setId(wareHouse.getId());
            vo.setName(wareHouse.getName());
            vo.setProvince(provinceMapper.findProvince(provinceId));
            vo.setCity(cityMapper.findCity(cityId));
            vo.setAddress(wareHouse.getAddress());
            vo.setPhone(wareHouse.getPhone());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<WarehouseVO> findSameCity(int cityId) {
        List<Integer> cities = cityMapper.findCities(cityMapper.findProvinceId(cityId));
        List<WarehouseVO> voList = new ArrayList<>();
        for (Integer id:cities){
            WarehouseVO vo = new WarehouseVO();
            WareHouse wareHouse = warehouseMapper.findByCity(id);
            int provinceId = cityMapper.findProvinceId(cityId);
            vo.setId(wareHouse.getId());
            vo.setName(wareHouse.getName());
            vo.setProvince(provinceMapper.findProvince(provinceId));
            vo.setCity(cityMapper.findCity(cityId));
            vo.setAddress(wareHouse.getAddress());
            vo.setPhone(wareHouse.getPhone());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<WarehouseVO> findAllEnabled(int page) {
        // 默认每页显示十条数据
        List<WareHouse> wareHouseList = warehouseMapper.findAllEnable(10*(page-1));
        List<WarehouseVO> voList = new ArrayList<>();
        for (WareHouse wareHouse:wareHouseList){
            WarehouseVO vo = new WarehouseVO();
            int cityId = wareHouse.getCityId();
            int provinceId = cityMapper.findProvinceId(cityId);
            vo.setId(wareHouse.getId());
            vo.setName(wareHouse.getName());
            vo.setProvince(provinceMapper.findProvince(provinceId));
            vo.setCity(cityMapper.findCity(cityId));
            vo.setAddress(wareHouse.getAddress());
            vo.setPhone(wareHouse.getPhone());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void update(int id, WarehouseDTO warehouseDTO) {

        warehouseMapper.update(id,warehouseDTO.getName(),warehouseDTO.getCityId(),
                warehouseDTO.getAddress(),warehouseDTO.getPostCode(),warehouseDTO.getPhone(),
                warehouseDTO.getStatus(), LocalDateTime.now());
    }

    @Override
    public void add(WarehouseDTO warehouseDTO) {
        WareHouse wareHouse = new WareHouse();

        wareHouse.setName(warehouseDTO.getName());
        wareHouse.setCityId(warehouseDTO.getCityId());
        wareHouse.setAddress(warehouseDTO.getAddress());
        wareHouse.setPostcode(warehouseDTO.getPostCode());
        wareHouse.setPhone(warehouseDTO.getPhone());
        wareHouse.setStatus(warehouseDTO.getStatus());
        wareHouse.setCreateTime(LocalDateTime.now());
        wareHouse.setLastUpdateTime(LocalDateTime.now());

        warehouseMapper.insert(wareHouse);
    }

    @Override
    public void del(int id) {
        WareHouse wareHouse;
        QueryWrapper<WareHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wareHouse = warehouseMapper.selectOne(wrapper);

        if (wareHouse == null||wareHouse.getStatus() == 0){
            throw new ParamErrorException("参数错误");
        }

        warehouseMapper.del(id,LocalDateTime.now());
    }

    @Override
    public void enable(int id) {
        WareHouse wareHouse;
        QueryWrapper<WareHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wareHouse = warehouseMapper.selectOne(wrapper);

        if (wareHouse == null || wareHouse.getStatus() == 1){
            throw new ParamErrorException("参数错误");
        }

        warehouseMapper.enable(id,LocalDateTime.now());
    }
}
